﻿// Copyright (c) Kona Framework Team. All rights reserved.
// For continued development and updates, visit http://www.codeplex.com/kona
// For a copy of the license please vist: http://www.codeplex.com/kona/license

using System;
using Kona.Data.Provider;
using AdomdClient = Microsoft.AnalysisServices.AdomdClient;
using System.Data;

namespace Kona.Data
{
    public class AdoMdConnection:IAdoMdConnection
    {
        private readonly Provider.IAdoMdConnectionPoolFactory poolFactory;
        private AdomdClient.AdomdConnection internalConnection;

        public AdoMdConnection(IAdoMdConnectionPoolFactory poolFactory)
        {
            this.poolFactory = poolFactory;
        }

        public AdoMdConnection(string connectionString)
        {
            poolFactory = AdoMdConnectionPoolFactory.Instance;
            internalConnection = poolFactory.GetConnectionPool(connectionString).GetObject();
        }

        internal AdomdClient.AdomdConnection InternalConnection
        { get { return this.internalConnection; } }

        #region Implementation of IDisposable

        void IDisposable.Dispose()
        {
            ((IDbConnection)this).Close();
            poolFactory.GetConnectionPool(this.ConnectionString).ReleaseObject(this.internalConnection);
        }

        #endregion

        #region Implementation of IDbConnection

        IDbTransaction IDbConnection.BeginTransaction()
        {
            throw new System.NotImplementedException();
        }

        IDbTransaction IDbConnection.BeginTransaction(IsolationLevel il)
        {
            throw new System.NotImplementedException();
        }

        public void Close()
        {
            this.internalConnection.Close();
        }

        void IDbConnection.ChangeDatabase(string databaseName)
        {
            throw new System.NotImplementedException();
        }

        public IDbCommand CreateCommand()
        {
            return this.internalConnection.CreateCommand();
        }

        public void Open()
        {
            // It should be open, unless it expired
            if(this.internalConnection.State != System.Data.ConnectionState.Open)
            {
                this.internalConnection.Open();
            }
        }

        public string ConnectionString
        {
            get { return this.internalConnection.ConnectionString; }
            set
            {
                if(internalConnection != null)
                {
                    throw  new InvalidOperationException("Connection Already Exists");
                }
                internalConnection = poolFactory.GetConnectionPool(value).GetObject();
            }
        }

        public int ConnectionTimeout
        {
            get { return this.internalConnection.ConnectionTimeout; }
        }

        public string Database
        {
            get { return this.internalConnection.Database; }
        }

        public ConnectionState State
        {
            get { return this.internalConnection.State; }
        }

        #endregion

        #region Implementation of IAdoMdConnection

        public string ClientVersion
        {
            get { return internalConnection.ClientVersion; }
        }

        public AdomdClient.CubeCollection Cubes
        {
            get { return internalConnection.Cubes; }
        }

        public AdomdClient.MiningModelCollection MiningModels
        {
            get { return internalConnection.MiningModels; }
        }

        public AdomdClient.MiningServiceCollection MiningServices
        {
            get { return internalConnection.MiningServices; }
        }

        public AdomdClient.MiningStructureCollection MiningStructures
        {
            get { return internalConnection.MiningStructures; }
        }

        public string ProviderVersion
        {
            get { throw new System.NotImplementedException(); }
        }

        public string ServerVersion
        {
            get { return internalConnection.ServerVersion; }
        }

        public string SessionID
        {
            get { return internalConnection.SessionID; }
            set { internalConnection.SessionID = value; }
        }

        public bool ShowHiddenObjects
        {
            get { return internalConnection.ShowHiddenObjects; }
            set { internalConnection.ShowHiddenObjects = value; }
        }

        #endregion
    }
}
